Inline Conditionally add key value pairs in Object

conditionally add in a key value pair or nothing at all. Avoid null or undefined key values being added to the object.


const condition = true

const myObject = {
  ...(condition ? { key: value } : {})
};


Credits